﻿using GameServer.Core;
using GameServer.Model;
using Serilog;
using Summer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GameServer.Mgr
{
    public class Spawner
    {
        public SpawnDefine Define;
        public Space Space;
        public Monster mon;
        public Vector3Int Pos;
        public Vector3Int Dir;
        private bool reviving=false;
        private float reviveTime;
        public Spawner(SpawnDefine define, Space space)
        {
            Define = define;
            Space = space;
            Pos = GetVector3(define.Pos);
            Dir = GetVector3(define.Dir);
            spawn();
        }
        private Vector3Int GetVector3(string text)
        {
            string aa = text.Substring(1, text.Length - 2);
            string[] bb = aa.Split(',');
            for (int i = 0; i < bb.Length; i++)
            {
                int x = int.Parse(bb[0]);
                int y = int.Parse(bb[1]);
                int z = int.Parse(bb[2]);
                return new Vector3Int(x, y, z);
            }
            return Vector3Int.zero;
        }
        private Vector3Int ParsePoint(string text)
        {
            string pattern = @"\[(\d+),(\d+),(\d+)\]";
            Match match = Regex.Match(text, pattern);
            if (match.Success)
            {
                int x = int.Parse(match.Groups[1].Value);
                int y = int.Parse(match.Groups[2].Value);
                int z = int.Parse(match.Groups[3].Value);
                return new Vector3Int(x, y, z);
            }
            return Vector3Int.zero;
        }
        private void spawn()
        {
            mon= this.Space.monsterManager.Create(this.Space.allMonsterInThisSpace,Define.TypeID,Pos, Dir);
        }
        public void Update()
        {
            if(mon!=null&&mon.IsDead&& !reviving)
            {
                this.reviveTime = Time.time + Define.Period;
                reviving = true;
            }
            if(reviveTime < Time.time&& reviving)
            {
                mon?.Revive();
                reviving = false;
            }
        }

    }
}
